﻿using System;
using System.Collections.Specialized;
using System.Data;
using System.Text;
using System.Web.UI;
using AMS.WorkBill.Manager;
using VitCode.BaseDataPageCreator;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;
using VitCode.Web.UI;

namespace AMS.Web.WorkBill
{
    public partial class WoList : Page
    {
        private readonly CreateControlHelper m_CreateCtlHlp = new CreateControlHelper();
        private PageBuilder m_PageBuilder;
        readonly PermissionManager psManager = new PermissionManager();

        private UserDataSourceBuilder DataSourceBuilder
        {
            get { return (UserDataSourceBuilder)Session["DataSourceBuilder"]; }
            set { Session["DataSourceBuilder"] = value; }
        }

        public string SettingXmlPath
        {
            get { return AppDomain.CurrentDomain.BaseDirectory + "Setting\\WoSetting.xml"; }
        }

        private string ClassName
        {
            get { return m_CreateCtlHlp.ReturnQueryStringValue("ClassName", this); }
        }

        private string TypeName
        {
            get { return m_CreateCtlHlp.ReturnQueryStringValue("TypeName", this); }
        }

        private string MenuId
        {
            get { return m_CreateCtlHlp.ReturnQueryStringValue("MenuId", this); }
        }

        

        private string QueryCondition
        {
            get
            {
                if (ViewState["QueryCondition"] != null)
                    return ViewState["QueryCondition"].ToString();
                else
                    return "";
            }
            set { ViewState["QueryCondition"] = value; }
        }


        protected override void OnInit(EventArgs e)
        {
            if (m_PageBuilder == null)
                m_PageBuilder = new PageBuilder(this, SettingXmlPath);
            NavigateCtl1.SettingXmlPath = SettingXmlPath;
            if (TypeName.Length > 0)
                m_PageBuilder.SetPgListAttribute(pgList, ClassName, TypeName);
            else
                m_PageBuilder.SetPgListAttribute(pgList, ClassName);
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            new WoPageBuilder(Page,SettingXmlPath).ClearCurrWoInfo(Page);
            pgList.DataSourceBuilderDelegate = new PaginationGrid.GridDataSourceBuilderDelegate(BindPgList);
            if (!IsPostBack)
            {
                m_CreateCtlHlp.SetDdlCondition(pgList, ddlCondition);
                btnAdd.Enabled = psManager.CheckPageIsCanAdd(this);
            }
        }

        private void ListBind()
        {
            pgList.BindGrid();
            bool isCanDelete = (DataSourceBuilder.PrimaryKeys != null && DataSourceBuilder.PrimaryKeys.Count > 0);
            btnRealDelete.Enabled = isCanDelete;
            btnRealDelete.Enabled = psManager.CheckPageIsCanDelete(this);
        }

        private UserDataSourceBuilder BindPgList()
        {
            string sql = m_PageBuilder.GetPgListSqlValue(ClassName, TypeName);
            sql = sql.Replace("\r\n", " ").Replace("\t", " ");
            if (sql.IndexOf("{0}") > -1)
                sql = sql.Replace("{0}", TypeName);
            sql += " " + QueryCondition;
            DataSourceBuilder = new UserDataSourceBuilder(sql, pgList);
            return DataSourceBuilder;
        }

        protected void pgList_OnGridItemSelect(object sender, PaginationGridItemSelectArgs e)
        {
            Response.Redirect("BaseInfo.aspx?MenuId=" + MenuId + "&ClassName=" + ClassName + "&TypeName=" + TypeName + "&" +
                              m_PageBuilder.GetPageKeyField(ClassName) + "=" +
                              e.SelectedDataKeyValue, false);
        }
       
        protected void btnRealDelete_Click(object sender, EventArgs e)
        {
            string strDelVal = pgList.GetSelectedItem(1);
            if (string.IsNullOrEmpty(strDelVal.Trim()))
            {
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "008", "<script>alert('请选择要删除的工单!')</script>");
                 return;
            }
            WorkBillManager manager = new WorkBillManager();
            StringCollection scErrCodes = manager.DeleteWorkBilllInfos(pgList.GetSelectedItem(1));
            if (scErrCodes.Count == 0)
                 ClientScript.RegisterStartupScript(ClientScript.GetType(), "009", "<script>alert('删除成功!')</script>");
                 
            else
            {
                var sb = new StringBuilder();
                sb.Append("部分数据不能删除,原因如下:\r\n");
                for (int i = 0; i < scErrCodes.Count; i++)
                {
                    string str = "待删除工单:["+scErrCodes[i]+"]，已经关闭不能删除";
                    sb.Append(str + "\r\n");
                }
                Alert.MessageBox(sb.ToString());
            }
            ListBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string editUrl = "BaseInfo.aspx?MenuId=" + MenuId + "&ClassName=" + ClassName;
            if (TypeName.Length > 0)
                editUrl += "&TypeName=" + TypeName;
            editUrl += "&Operate=Add";
            Response.Redirect(Server.UrlPathEncode(editUrl));
        }

        protected void ConditionSelector1_SelectCondition(SelectConditionEventArgs e)
        {
            QueryCondition = null;
            if (ConditionSelector1.SelectedConditionString.Length > 0)
                QueryCondition = " where " + ConditionSelector1.SelectedConditionString;
            ListBind();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            QueryCondition = QueryHelper.GetQueryCondition(ddlCondition.SelectedValue, txtValue.Text, rbtAmount.Checked);
            ListBind();
        }
    }
}
